#!/usr/bin/perl -w
use strict;
use warnings;
use DBI;

require('function.pl');

my @domains;
open IN, 'data/spam_domain' or die("fail	open data/spam_domain\n");
while (<IN>) {
	chop;
	push @domains, $_ if (length() > 5);
}
close IN;

my $db_conn = DBI->connect("DBI:mysql:database=phone_num_search;host=localhost", 'root', '');
$db_conn->do('set names utf8');
$db_conn->{'mysql_use_result'} = 1;
my $sql = 'select phone_num, rank, url from result';
my $request = $db_conn->prepare($sql);
my $result = $request->execute();
while (my ($phone_num, $rank, $url) = $request->fetchrow_array) {
	my @arr = split('/', $url);
	my $match = 0;
	if (@arr < 3) {
#		print "$url\n";
		$arr[2] = $arr[0];
	}
	if ($arr[0] eq '.') {
		print "$phone_num\t$rank\t$url\n";
		next;
	}
	foreach my $domain (@domains) {
		my $pos = index($arr[2], $domain);
#		if ($pos >= 0 && $pos == length($arr[2]) - length($domain)) {
		if ($pos >= 0) {
			$sql = "update result set spam = 1 where phone_num = '$phone_num' and rank = $rank";
			$sql = "delete result where phone_num = '$phone_num' and rank = $rank";
#			$db_conn->do($sql);
			print "$phone_num\t$rank\t$url\t$domain\n";
			$match = 1;
			last;
		}
	}
=c
	if ($match == 0) {
		print "$arr[2]\t$url\n";
	}
	if (!$match) {
		$sql = "update result set spam = 2 where phone_num = '$phone_num' and rank = $rank";
#		$db_conn->do($sql);
#		print "$url\n";
	}
=cut
}
